package org.amse.Archiver.view;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.File;
import java.net.URL;
import java.util.ArrayList;
import javax.swing.*;
import org.amse.Archiver.model.*;
@SuppressWarnings("serial")
public class View extends JFrame implements IView {
	private IModel myModel;
	private Container container = getContentPane();
    private JLabel addressBar;
	private JTable myTable;
	private final URL encodePic =ClassLoader.getSystemResource("data/encode.png");
	private final URL extractPic = ClassLoader.getSystemResource("data/extract.png");
	//private final URL testPic = ClassLoader.getSystemResource("data/test.png");
	private final URL rootPic = ClassLoader.getSystemResource("data/over.png");
	private SystemViewModel myTableModel;
	
	public View() {
		myModel = new Model(this);
		setSize(700, 500);
		setMinimumSize(new Dimension(400, 300));
		setDefaultCloseOperation(EXIT_ON_CLOSE);
		container.setLayout(new BorderLayout());
		container.add(createMenu(), BorderLayout.NORTH);		
		JPanel jp1 = new JPanel(new BorderLayout());
		jp1.add(createQuickMenu(), BorderLayout.NORTH);
		JPanel jp2 = new JPanel(new BorderLayout());
		jp2.add(createAddressTool(), BorderLayout.NORTH);
		jp2.add(new JScrollPane(createTable()));
		jp1.add(jp2);
		container.add(jp1);
		update();
		
	}
    public SystemViewModel getTableModel(){
    	return myTableModel;
    } 
	public JMenuBar createMenu(){
		JMenuBar menuBar = new JMenuBar();
		JMenu fileMenu = new JMenu("File");
		JMenu helpMenu = new JMenu("Help");
		final JMenuItem addFile = new JMenuItem("Archive file");
		final JMenuItem extractFile = new JMenuItem("Extract file");
		final JMenuItem about = new JMenuItem("About");
		addFile.addActionListener(new ComressAction(this));
		extractFile.addActionListener(new DecompressAction(this));
		
		about.addActionListener(new ActionListener(){
			public void actionPerformed(ActionEvent e) {
				JOptionPane.showMessageDialog(about, "Designed for amse by Lavrenko Anastasia 2008-2009.");
			}
		});
		
   		fileMenu.add(addFile);
		fileMenu.add(extractFile);
		helpMenu.add(about);
		menuBar.add(fileMenu);
		menuBar.add(helpMenu);
		return menuBar;
	}
	public JToolBar createQuickMenu(){
		JToolBar toolBar = new JToolBar();
		final JButton encodeButton = new JButton(new ImageIcon(encodePic));
		final JButton extractButton = new JButton(new ImageIcon(extractPic));
		//final JButton testButton = new JButton(new ImageIcon(testPic));
		encodeButton.addActionListener(new ComressAction(this));
		extractButton.addActionListener(new DecompressAction(this));
		/*
		testButton.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				JOptionPane.showMessageDialog(testButton, "Not Supported yet!");
			}
		});
		*/
		toolBar.add(encodeButton);
		toolBar.add(extractButton);
		//toolBar.add(testButton);
		return toolBar;
	}
	public JTable createTable() {
		myTableModel = new SystemViewModel();
		myTable = new JTable(myTableModel);
		myTable.addMouseListener(new myListener(this));
		myTable.setAutoCreateRowSorter(true);
		return myTable;
	}
	public JToolBar createAddressTool(){
		JToolBar addressTool = new JToolBar();
		final JButton overButton = new JButton(new ImageIcon(rootPic));
		overButton.addActionListener(new ActionListener(){
			public void actionPerformed(ActionEvent e) {
				myModel.changeDir(0);
				update();
			}
		});
		
		addressBar = new JLabel(myModel.getCurrentDir().getAbsolutePath());
		
		addressTool.add(overButton);
		addressTool.add(addressBar);
		return addressTool;
	}
	public void update() {
		ArrayList<File> v = new ArrayList<File>();
	   	v.add(0, new File(".."));
	   	v.addAll(myModel.getContentDir());
	   	myTableModel.addData(v);
	}

	public void update(int index) {
		myModel.changeDir(index);
        addressBar.setText(myModel.getCurrentDir().toString());
		update();
	}
	
	 public int getSelectedRow(){
			return myTable.getSelectedRow();
	}
	 
	public IModel getModel(){
		return myModel;
	}
	
}
